package com.cz.video.website.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cz.video.common.constant.UserConstants;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.website.domain.WebsiteMsg;
import com.cz.video.website.domain.WebsiteMsg;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.video.website.domain.bo.WebsiteMsgBo;
import com.cz.video.website.domain.bo.WebsiteMsgBo;
import com.cz.video.website.domain.vo.WebsiteMsgVo;
import com.cz.video.website.mapper.WebsiteMsgMapper;
import com.cz.video.website.service.IWebsiteMsgService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LLF
 * @since 2023-10-16
 */
@Service
@RequiredArgsConstructor
public class WebsiteMsgServiceImpl extends ServiceImpl<WebsiteMsgMapper, WebsiteMsg> implements IWebsiteMsgService {
    private final WebsiteMsgMapper websiteMsgMapper;

    /**
     * 新增保存信息表信息
     *
     * @param websiteMsg 用户信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertMsg(WebsiteMsg websiteMsg) {
        int rows = websiteMsgMapper.insert(websiteMsg);
        return rows;
    }

    /**
     * 修改信息表信息
     * @param websiteMsg
     * @return
     */
    @Override
    public int updateMsg(WebsiteMsgBo websiteMsg) {
        return websiteMsgMapper.updateById(websiteMsg);
    }

    /**
     * 分页查找信息表信息
     *
     * @param websiteMsg
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<WebsiteMsgVo> selectPageMsgList(WebsiteMsgBo websiteMsg, PageQuery pageQuery) {
        Page<WebsiteMsgVo> page=websiteMsgMapper.selectPageMsgList(pageQuery.build(),this.buildQueryWrapper(websiteMsg));
        return TableDataInfo.build(page);
    }

    @Override
    public int deleteById(int id) {
        return websiteMsgMapper.deleteById(id);
    }

    /**
     * 根据查询条件构建query wrapper
     *
     * @param websiteMsg 查询条件
     *
     * @return query wrapper
     */
    private Wrapper<WebsiteMsg> buildQueryWrapper(WebsiteMsgBo websiteMsg) {
        Map<String, Object> params = websiteMsg.getParams();
        QueryWrapper<WebsiteMsg> wrapper = Wrappers.query();

        wrapper.eq("wm.del_flag", UserConstants.USER_NORMAL)
            .eq(ObjectUtil.isNotNull(websiteMsg.getMsgId()), "wm.Msg_id", websiteMsg.getMsgId())
            .eq(ObjectUtil.isNotNull(websiteMsg.getAccountId()), "wm.Account_id", websiteMsg.getAccountId())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "wm.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }





}
